<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Admin\Protype;
use Illuminate\Support\Facades\Input;
class ProtypeController extends Controller
{
    //列表
    public function index(){
    	//获取数据
    	$data = Protype::orderBy('sort','desc') -> get();
    	//展示视图，携带数据
    	return view('admin.protype.index',['data' => $data]);
    }

    public function add(Request $request){
        if($request->isMethod('post')){
            $data = Input::all();
            empty($data['sort']) ? $data['sort'] = 0 : $data['sort'] = $data['sort'];
            $result = Protype::insert([
                'protype_name'	=>	$data['protype_name'],
                'sort'	        =>	$data['sort'],
                'created_at'    =>	date('Y-m-d H:i:s'),
            ]);
            //返回结果
            return $result ? '1' : '0';
        }else{
            return view('admin.protype.add');
        }
    }

    public function edit(Request $request){
        if($request->isMethod('post')){
            $data = Input::all();
            empty($data['sort']) ? $data['sort'] = 0 : $data['sort'] = $data['sort'];
            $result = Protype::where('id',$data['id']) -> update([
                'protype_name'	=>	$data['protype_name'],
                'sort'	        =>	$data['sort'],
                'status'        => $data['status'],
                'updated_at'    => date('Y-m-d H:i:s'),
            ]);
            //返回结果
            return $result ? '1' : '0';
        }else{
            $id = Input::get('id');
            $data = Protype::where('id',$id) -> first();
            return view('admin.protype.edit',['data' => $data]);
        }

    }

    public function del(){
        $id = Input::get('id');
        $res = Protype::where('id',$id) -> delete();
        $res ? $data['status'] = 200 :  $data['status'] = 0;
        return response() -> json($data);
    }

    public function search(){
        $keyword = Input::get('key');
        $data = Protype::where('protype_name','LIKE',"%$keyword%") -> orderBy('sort','desc') -> get();
        //展示视图，携带数据
        return view('admin.protype.index',['data' => $data, 'key' => $keyword]);
    }
}
